package com.relsell.view;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.relsell.pointofsales.R;
import com.relsell.utility.AppVars;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class EditProfile extends Fragment implements OnClickListener {

	public EditProfile() {
	}

	public static final String CHANGE_PASSWORD = "1";
	EditText oldpassword, newpassword;
	Button submit, clear;
	AsyncTask<Void, Void, Void> mRegisterTask;
	String responseString;
	String oldpass, newpass;
	TextView user, error;
	String data;
	String oldpasswordd = null, passwordchanged = null;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.editprofile, container, false);
		oldpassword = (EditText) v.findViewById(R.id.etOldPass);
		newpassword = (EditText) v.findViewById(R.id.etNewPass);
		submit = (Button) v.findViewById(R.id.btnSubmit);
		clear = (Button) v.findViewById(R.id.btnClear);
		user = (TextView) v.findViewById(R.id.tvUser);
		error = (TextView) v.findViewById(R.id.textViewError);
		data = this.getActivity().getIntent().getExtras().getString("username");
		user.setText(data);
		submit.setOnClickListener(this);
		clear.setOnClickListener(this);
		return v;

	}

	@Override
	public void onClick(View arg0) {

		oldpass = oldpassword.getText().toString();
		newpass = newpassword.getText().toString();

		switch (arg0.getId()) {
		case R.id.btnSubmit:

			if (!oldpass.isEmpty() && !newpass.isEmpty()) {

				this.getActivity().setProgressBarIndeterminateVisibility(true);

				mRegisterTask = new AsyncTask<Void, Void, Void>() {

					@Override
					protected Void doInBackground(Void... params) {
						try {

							HttpClient http = new DefaultHttpClient();
							String server = AppVars.getServer();
							HttpPost post = new HttpPost(
									"http://"
											+ server
											+ "/gcminteraction/view/start.php?controlVar=12");
							List<NameValuePair> data = new ArrayList<NameValuePair>();
							Log.w("old", oldpass);
							Log.w("new", newpass);
							data.add(new BasicNameValuePair("userid",
									AppVars.sloggedInUserId));
							data.add(new BasicNameValuePair("oldpassword",
									oldpass));
							data.add(new BasicNameValuePair("newpassword",
									newpass));
							data.add(new BasicNameValuePair("profileop",
									CHANGE_PASSWORD));
							post.setEntity(new UrlEncodedFormEntity(data));

							HttpResponse response = http.execute(post);
							responseString = new BasicResponseHandler()
									.handleResponse(response);

							Log.w("Response String =", responseString);

							JSONObject obj;
							try {
								obj = new JSONObject(responseString);

								if (obj.isNull("passwordchanged")) {
									oldpasswordd = obj.getString("oldpassword");
									passwordchanged="";
									

								} else if (obj.isNull("oldpasswordd")) {
									passwordchanged = obj
											.getString("passwordchanged");
									oldpasswordd="";
								}

							} catch (JSONException e) {
								e.printStackTrace();
							}

						} catch (ClientProtocolException e) {

						} catch (IOException e) {

						}
						return null;

					}

					@Override
					protected void onPostExecute(Void result) {
						getActivity().setProgressBarIndeterminateVisibility(
								false);

						if (oldpasswordd.equals("10")) {
							error.setText(getResources().getString(R.string.wrong_password_txt));
							oldpassword.setText("");
							newpassword.setText("");
							Toast.makeText(getActivity(),
									"Profile Change Failed", Toast.LENGTH_LONG)
									.show();
						} else if (passwordchanged.equals("1")) {
							oldpassword.setText("");
							newpassword.setText("");
							Toast.makeText(getActivity(),
									"Password Changed Successfully",
									Toast.LENGTH_LONG).show();
						}
					}

				};
				mRegisterTask.execute(null, null, null);

			} else {
				error.setText("Field cannot be left blank");
			}

			break;

		case R.id.btnClear:
			oldpassword.setText("");
			newpassword.setText("");
			break;

		}

	}

}